//
//  DawnRect.swift
//  DawnFoundation
//
//  Created by 崔成成 on 2021/8/5.
//

import UIKit

struct DawnRect {
    
    var width: CGFloat?
    {
        didSet {
            guard let width = width else { return }
            if minX != nil { return }
            if let maxX = maxX {
                minX = maxX - width
            }else if let midX = midX {
                minX = midX - width/2
            }
        }
    }
    var height: CGFloat?
    {
        didSet {
            guard let height = height else { return }
            if minY != nil { return }
            if let maxY = maxY {
                minY = maxY - height
            }else if let midY = midY {
                minY = midY - height/2
            }
        }
    }
    var minX: CGFloat?
    {
        didSet {
            guard let minX = minX else { return }
            if width != nil { return }
            if let midX = midX {
                width = (midX - minX) * 2
            }else if let maxX = maxX {
                width = maxX - minX
            }
        }
    }
    var minY: CGFloat?
    {
        didSet {
            guard let minY = minY else { return }
            if height != nil { return }
            if let midY = midY {
                height = (midY - minY) * 2
            }else if let maxY = maxY {
                height = maxY - minY
            }
        }
    }
    var midX: CGFloat?
    {
        didSet {
            guard let midX = midX else { return }
            if let width = width {
                minX = midX - width/2
            }else if let minX = minX {
                width = (midX - minX) * 2
            }else if let maxX = maxX {
                width = (maxX - midX) * 2
                minX = maxX - width!
            }
            
        }
    }
    var midY: CGFloat?
    {
        didSet {
            guard let midY = midY else { return }
            if let height = height {
                minY = midY - height/2
            }else if let minY = minY {
                height = (midY - minY) * 2
            }else if let maxY = maxY {
                height = (maxY - midY) * 2
                minY = maxY - height!
            }
            
        }
    }
    var maxX: CGFloat?
    {
        didSet {
            guard let maxX = maxX else { return }
            if let width = width {
                minX = maxX - width
            }else if let minX = minX {
                width = maxX - minX
            }else if let midX = midX {
                width = (maxX - midX) * 2
                minX = maxX - width!
            }
        }
    }
    var maxY: CGFloat?
    {
        didSet {
            guard let maxY = maxY else { return }
            
            if let height = height {
                minY = maxY - height
            }else if let minY = minY {
                height = maxY - minY
            }else if let midY = midY {
                height = (maxY - midY) * 2
                minY = maxY - height!
            }
        }
    }
    
   mutating func dawn(minX: CGFloat? = nil,
              minY: CGFloat? = nil,
              maxX: CGFloat? = nil,
              maxY: CGFloat? = nil,
              midX: CGFloat? = nil,
              midY: CGFloat? = nil,
              width: CGFloat? = nil,
              height: CGFloat? = nil) -> CGRect {
    
        if minX != nil {
            self.minX = minX
        }
        if minY != nil {
            self.minY = minY
        }
        if midX != nil {
            if self.minX != nil && self.width != nil && self.width != 0 {
                self.minX = nil
            }
            self.midX = midX
        }
        if midY != nil {
            if self.minY != nil && self.height != nil && self.height != 0 {
                self.minY = nil
            }
            self.midY = midY
        }
        if maxX != nil {
            if self.minX != nil && self.width != nil && self.width != 0 {
                self.minX = nil
            }
            self.maxX = maxX
        }
        if maxY != nil {
            if self.minY != nil && self.height != nil && self.height != 0 {
                self.minY = nil
            }
            self.maxY = maxY
        }
        if width != nil {
            self.width = width
        }
        if height != nil {
            self.height = height
        }
        return CGRect(x: self.minX ?? 0, y: self.minY ?? 0, width: self.width ?? 0, height: self.height ?? 0)
    }
    
    mutating func updateVertical(minY: CGFloat? = nil,
                                   maxY: CGFloat? = nil,
                                   midY: CGFloat? = nil,
                                   height: CGFloat? = nil) -> CGRect {
     
        
        self.minY = nil
        self.midY = nil
        self.maxY = nil
        self.height = nil
        
        if minY != nil {
            self.minY = minY
        }
        if midY != nil {
            self.midY = midY
        }
        if maxY != nil {
            self.maxY = maxY
        }
        if height != nil {
            self.height = height
        }
        
//        self.minY = minY
//        self.midY = midY
//        self.maxY = maxY
//        self.height = height
         return CGRect(x: self.minX ?? 0, y: self.minY ?? 0, width: self.width ?? 0, height: self.height ?? 0)
    }
    
    mutating func updateHorizontal(minX: CGFloat? = nil,
               maxX: CGFloat? = nil,
               midX: CGFloat? = nil,
               width: CGFloat? = nil) -> CGRect {
        
        self.minX = nil
        self.midX = nil
        self.maxX = nil
        self.width = nil
        
        if minX != nil {
            self.minX = minX
        }
        if midX != nil {
            self.midX = midX
        }
        if maxX != nil {
            self.maxX = maxX
        }
        if width != nil {
            self.width = width
        }
        
//        self.minX = minX
//        self.midX = midX
//        self.maxX = maxX
//        self.width = width
         return CGRect(x: self.minX ?? 0, y: self.minY ?? 0, width: self.width ?? 0, height: self.height ?? 0)
     }
    
    mutating func clear() {
        self.minX = nil
        self.midX = nil
        self.maxX = nil
        self.width = nil
        self.minY = nil
        self.midY = nil
        self.maxY = nil
        self.height = nil
    }
}
